using System.Text.Json.Serialization;

namespace Crux.Core.Payloads;
/// <summary>
/// Payload
/// </summary>
public record Payload : IPayload
{
    #region .ctor
    /// <summary>
    /// 错误提醒
    /// </summary>
    protected Payload(
        Result result = Result.Error,
        string? message = null)
    {
        Result = result;
        Message = message;
    }
    #endregion .ctor

    #region implements
    /// <inheritdoc/>
    [JsonPropertyName("result")]
    public Result Result { get; init; }
    /// <inheritdoc/>
    [JsonPropertyName("message")]
    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public string? Message { get; init; }

    #endregion implements

    /// <summary>
    /// 成功
    /// </summary>
    /// <returns>Payload</returns>
    public static Payload Success() => new Payload(result: Result.Success);
    /// <summary>
    /// 失败
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload Error(string message) => new Payload(message: message);
    /// <summary>
    /// 资源不存在
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload NotFound(string message) => new Payload(result: Result.NotFound, message: message);
    /// <summary>
    /// 无权限
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload Unauthorized(string message) => new Payload(result: Result.Unauthorized, message: message);
    /// <summary>
    /// 禁止访问
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload Forbidden(string message) => new Payload(result: Result.Forbidden, message: message);
    /// <summary>
    /// 更新冲突
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload Conflict(string message)
    => new Payload(result: Result.Conflict, message: message);
    /// <summary>
    /// 服务器错误
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload InternalServerError(string message)
    => new Payload(result: Result.InternalServerError, message: message);
    /// <summary>
    /// 锁定
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>Payload</returns>
    public static Payload Locked(string message)
    => new Payload(result: Result.Locked, message: message);

}